package cn.hy.preparelesson.common.service;

import cn.hy.preparelesson.common.entity.PlLessonClassDetail;
import cn.hy.preparelesson.common.entity.PlLessonClassDetailExample;
import cn.hy.preparelesson.common.exception.MessageException;
import cn.hy.preparelesson.common.mapper.PlLessonClassDetailMapper;
import cn.hy.preparelesson.common.utils.IdUtils;
import cn.hy.preparelesson.common.vo.ClassVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 备课-班级
 */
@Service
public class PlLessonClassDetailService extends BaseService{
    @Autowired
    PlLessonClassDetailMapper plLessonClassDetailMapper;

    /***
     * 批量保存备课与班级信息
     * @param plLessonClassDetails
     */
    @Transactional(rollbackFor = Exception.class)
    public void batchSavePlLessonClassDetail(List<PlLessonClassDetail> plLessonClassDetails){
        if (CollectionUtils.isEmpty( plLessonClassDetails)) {
            throw  new MessageException("请选择班级信息！");
        }
        plLessonClassDetailMapper.batchSavePlLessonClassDetail(plLessonClassDetails);
    }

    /**
     * 批量修改
     * @param schoolId
     * @param opeUserId
     * @param lessonId
     * @param classVoList
     * @param now
     */
    @Transactional(rollbackFor = Exception.class)
    public void batchUpdatePlLessonClassDetail(String schoolId, String opeUserId, String lessonId, List<ClassVo> classVoList, Date now) {
        batchDeletePlLessonClassDetail(schoolId, opeUserId, lessonId);
        List<PlLessonClassDetail> plLessonClassDetailList = new ArrayList<>();
        classVoList.stream().forEach(classVo -> {
            plLessonClassDetailList.add(new PlLessonClassDetail(IdUtils.newId(), lessonId, classVo.getClassId(),
                    classVo.getDistanceYear(), classVo.getGradeName(), classVo.getClassCode(), classVo.getClassName(), schoolId,  now, opeUserId));
        });
        batchSavePlLessonClassDetail( plLessonClassDetailList);
    }

    /**
     * 批量删除备课-班级信息
     * @param schoolId
     * @param opeUserId
     * @param lessonId
     */
    @Transactional(rollbackFor = Exception.class)
    public void batchDeletePlLessonClassDetail(String schoolId, String opeUserId, String lessonId) {
        PlLessonClassDetailExample example = new PlLessonClassDetailExample();
        example.createCriteria().andSchoolIdEqualTo(schoolId).andLessonIdEqualTo(lessonId)
                .andCreateByEqualTo(opeUserId);
        plLessonClassDetailMapper.deleteByExample(example);
    }

    /**
     * 查询 备课-班级 集合
     * @param schoolId
     * @param lessonId
     * @return
     */
    public List<PlLessonClassDetail> findPlLessonClassDetailsBySchoolIdAndLessonId(String schoolId, String lessonId) {
        PlLessonClassDetailExample example = new PlLessonClassDetailExample();
        example.createCriteria().andSchoolIdEqualTo(schoolId).andLessonIdEqualTo(lessonId);
        List<PlLessonClassDetail> plLessonClassDetails = plLessonClassDetailMapper.selectByExample(example);
        return   plLessonClassDetails;
    }

    /**
     * 根据schoolId lessonId distanceYear classId 集合 查询 备课 - 班级信息集合
     * @param schoolId
     * @param lessonIdList
     * @return
     */
    public List<PlLessonClassDetail> findPlCDListByLessonIdInAndBy(String schoolId, List<String> lessonIdList, BigDecimal distanceYear, String classId) {
        PlLessonClassDetailExample example = new PlLessonClassDetailExample();
        PlLessonClassDetailExample.Criteria criteria = example.createCriteria();
        criteria.andSchoolIdEqualTo(schoolId);
        if (distanceYear != null) {
            criteria.andDistanceYearEqualTo(distanceYear);
        }
        if (StringUtils.isNotBlank(classId)) {
            criteria.andClassIdEqualTo(classId);
        }
        if (!CollectionUtils.isEmpty(lessonIdList)) {
            criteria.andLessonIdIn(lessonIdList);
        }
        example.setOrderByClause("create_at desc");
       return plLessonClassDetailMapper.selectByExample(example);
    }
}
